/**
 * @file src/datatypes/Edge.java
 */
package datatypes;

/**
 * Class that represents an action
 */
public class Edge
{
    /** Costs to take this edge(action) */
    private float mStepCosts;

    /** Connected node(state) on the "left" */
    private final Node mLeftNode;

    /** Connected node(state) on the "right" */
    private final Node mRightNode;

    /** Hide default constructor */
    @SuppressWarnings("unused")
    private Edge()
    {
    	mLeftNode = null;
    	mRightNode = null;
    }

    /**
     *  Constructor
     *  
     *  @param costs The costs of this edge
     *  @param left The left neighbor node
     *  @param right The right neighbor node
     */
    public Edge(float costs, Node left, Node right) throws Exception
    {
        /** check left and right node */
        if (left == null || right == null) {
            throw new Exception("Try to create edge with empty ends");
        }
        /** assign costs */
        mStepCosts = costs;

        /** consistency check (not really necessary) */
        if (left.getState().compareTo(right.getState()) < 0) {
            mLeftNode = left;
            mRightNode = right;
        } else if (left.getState().compareTo(right.getState()) > 0) {
            mLeftNode = right;
            mRightNode = left;
        } else {
            throw new Exception("Edge(float costs, Node left, Node right) : Try to create an edge with two identicial ends");
        }
        
    } // Edge(float costs, Node left, Node right)

    /**
     *  Getter
     */
    public float getStepCost() { return mStepCosts; }
    
    /**
     * Override toString()
     * 
     * @return String representation of an edge
     */
    public String toString()
    {
        return "<" + mLeftNode.toString() + " <-> " + mRightNode.toString()
                + ", costs = " + mStepCosts + ">";
    }

    /**
     * Get possible adjacent neighbor
     * 
     * @param node Node to get adjacent edge's neighbor for
     * 
     * @return The adjacent neighbor or null if node is not an end of this edge
     */
    public Node getAdjacentNode(Node node)
    {
        if (mLeftNode.getState().compareTo(node.getState()) == 0) {
        	return mRightNode;
        } else if (mRightNode.getState().compareTo(node.getState()) == 0) {
        	return mLeftNode;
        }
        return null;
    }

} // class Edge
